//
//  TJOrderListScreeningController.m
//  Teleport
//
//  Created by hc-101 on 2020/6/12.
//  Copyright © 2020 xtjun. All rights reserved.
//筛选VC

#import "TJOrderListScreeningController.h"
#import "TJOrderListScreeningHeaderView.h"
#import "TJOrderListScreeningCell.h"
#import "TJOrderListScreeningModel.h"

@interface TJOrderListScreeningController ()<UIGestureRecognizerDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) UIView *maskView;
@property (weak, nonatomic) UIView *contentView;

@property (strong, nonatomic) UICollectionViewFlowLayout *flowLayout;
@property (strong, nonatomic) NSArray *datas;
@property (weak, nonatomic) UICollectionView *collectionView;
@property (assign, nonatomic) CGFloat leftMargin;
//@property (weak, nonatomic) UITextField *amountTextField;

@end


@implementation TJOrderListScreeningController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = UIColor.tj_alertMaskColor;
        self.contentView.tj_x = self.leftMargin;
    } completion:^(BOOL finished) {
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _leftMargin = ZOOM(60);
    
    UIView *maskView = [UIView tj_addToView:self.view];
    maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
    maskView.frame = self.view.bounds;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMaskView)];
    [maskView addGestureRecognizer:tap];
    tap.delegate = self;
    
    UIView *contentView = [UIView tj_addToView:maskView];
    contentView.backgroundColor = UIColor.whiteColor;
    contentView.userInteractionEnabled = YES;
    [maskView addSubview:contentView];
    contentView.frame = CGRectMake(KScreenWidth, 0, KScreenWidth - _leftMargin, KScreenHeight);
    
//    UILabel *amountLabel = [UILabel lableWithSuperView:contentView font:MediumFont(16) color:UIColor_Hex(0x3781E7) title:@"金额".localizedString textAlignment:NSTextAlignmentLeft];
//    [amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.offset(16);
//        make.top.offset(20 + StatusBarHeight);
//    }];
//
//    UIView *amountView = [UIView tj_addToView:contentView];
//    [amountView shearRoundedCornersWithRadiu:3];
//    amountView.layer.borderColor = UIColor.tj_separatorColor.CGColor;
//    amountView.layer.borderWidth = 1;
//    [amountView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(amountLabel);
//        make.right.offset(-16);
//        make.height.offset(34);
//        make.top.equalTo(amountLabel.mas_bottom).offset(15);
//    }];
//
//    UITextField *amountTextField = [UITextField tj_addToView:amountView];
//    amountTextField.placeholder = @"系统会为您筛选包含目标金额的商品".localizedString;
//    amountTextField.font = UIFont_Px(13);
//    amountTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//    amountTextField.keyboardType = UIKeyboardTypeNumberPad;
//    [amountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.offset(4);
//        make.bottom.right.top.offset(0);
//    }];
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.minimumLineSpacing = 16;
    self.flowLayout.minimumInteritemSpacing = 10;
    self.flowLayout.itemSize = CGSizeMake((contentView.tj_width - 32 - _leftMargin - self.flowLayout.minimumInteritemSpacing * 2) / 3.0 - 2, 30);
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 16, 10, 16);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [collectionView registerClass:TJOrderListScreeningCell.class forCellWithReuseIdentifier:orderListScreeningCell];
    [collectionView registerClass:TJOrderListScreeningHeaderView.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:orderListScreeningHeaderView];
    [contentView addSubview:collectionView];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(16 + StatusBarHeight);
        make.left.right.offset(0);
        make.height.offset(326);
    }];
    
    UIView *bottomView = [UIView tj_addToView:contentView];
    [bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(40);
    }];
    
    UIButton *resetButton = [UIButton buttonWithSuperView:bottomView font:UIFont_Px(14) color:UIColor.tj_mainColor title:@"重置".localizedString];
    resetButton.backgroundColor = UIColor.whiteColor;
    [resetButton addTarget:self action:@selector(didClickResetButton) forControlEvents:UIControlEventTouchUpInside];
    [resetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.top.offset(0);
        make.width.offset(contentView.tj_width * 0.5);
    }];
    
    UIButton *determineButton = [UIButton buttonWithSuperView:bottomView font:UIFont_Px(14) color:UIColor.whiteColor title:@"确定".localizedString];
    determineButton.backgroundColor = UIColor.tj_mainColor;
    [determineButton addTarget:self action:@selector(didClickDetermineButton) forControlEvents:UIControlEventTouchUpInside];
    [determineButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.top.offset(0);
        make.width.offset(contentView.tj_width * 0.5);
    }];
    
    _collectionView = collectionView;
    _contentView = contentView;
    _maskView = maskView;
//    _amountTextField = amountTextField;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (touch.view != self.maskView) {
        return NO;
    }else {
        return YES;
    }
}


- (void)tapMaskView {
    
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        self.contentView.tj_x = KScreenWidth;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

/**确定按钮 **/
- (void)didClickDetermineButton {
    NSMutableArray *selectArr = @[].mutableCopy;
    
    [self.datas enumerateObjectsUsingBlock:^(NSArray *sectionDatas, NSUInteger idx, BOOL * _Nonnull sectionStop) {
        [sectionDatas enumerateObjectsUsingBlock:^(TJOrderListScreeningModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.isSelect) {
                [selectArr addObject:obj];
                *stop = YES;
            }
        }];
    }];
    
    [self dismissViewControllerAnimated:NO completion:^{
        if (self.selectComplete) {
            self.selectComplete(selectArr);
        }
    }];
    
}
/**重置按钮 **/
- (void)didClickResetButton {
    [self.datas enumerateObjectsUsingBlock:^(NSArray *sectionDatas, NSUInteger idx, BOOL * _Nonnull sectionStop) {
        [sectionDatas enumerateObjectsUsingBlock:^(TJOrderListScreeningModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.isSelect = NO;
        }];
    }];
    
    [self.datas enumerateObjectsUsingBlock:^(NSArray <TJOrderListScreeningModel *>*sectionDatas, NSUInteger idx, BOOL * _Nonnull sectionStop) {
        sectionDatas.firstObject.isSelect = YES;
    }];
    
    [UIView setAnimationsEnabled:NO];
    [self.collectionView reloadData];
    [UIView setAnimationsEnabled:YES];
}
#pragma mark ---------------UICollectionViewDataSource, UICollectionViewDelegate------------------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.datas.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return [self.datas[section] count];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TJOrderListScreeningCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:orderListScreeningCell forIndexPath:indexPath];
    TJOrderListScreeningModel *model = self.datas[indexPath.section][indexPath.row];
    model.section = indexPath.section;
    cell.model = model;
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        TJOrderListScreeningHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:orderListScreeningHeaderView forIndexPath:indexPath];
        headerView.textLabel.text = (indexPath.section == 0) ? @"支付方式".localizedString : @"法币".localizedString;
        return headerView;
    }
    
    return nil;
    
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(SCREEN_WIDTH, 36);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray <TJOrderListScreeningModel *>*typeDatas = self.datas[indexPath.section];
    [typeDatas enumerateObjectsUsingBlock:^(TJOrderListScreeningModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.isSelect = NO;
    }];
    
    TJOrderListScreeningModel *model = typeDatas[indexPath.row];
    model.isSelect = YES;
    
    [UIView setAnimationsEnabled:NO];
    [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
    [UIView setAnimationsEnabled:YES];
    
}

- (NSArray *)datas {
    if (!_datas) {
        NSArray *dictArr = @[
            @[
                @{
                    @"text": @"全部".localizedString,
                    @"identifier": @"",
                    @"isSelect": @"1"
                },
                @{
                    @"text": @"微信".localizedString,
                    @"identifier": @"WECHAT",
                    @"isSelect": @"0"
                },
                @{
                    @"text": @"支付宝".localizedString,
                    @"identifier": @"ALIAY",
                    @"isSelect": @"0"
                },
                @{
                    @"text": @"银行卡".localizedString,
                    @"identifier": @"BANKCARD",
                    @"isSelect": @"0"
                },
            ]
            
        ];
        //支付方式 不传为全部订单收款方式,多种的话用-拼接(ALIAY,WECHAT,BANKCARD) 如 ALIAY-WECHAT
        _datas = [TJOrderListScreeningModel mj_objectArrayWithKeyValuesArray:dictArr];
    }
    return _datas;
}





@end
